/*
* qiudot.com Inc.
* Copyright (c) 2019 All Rights Reserved.
* create by qiudot
* date:2019-10-05
*/
package com.lora.web;

import com.acooly.core.common.web.AbstractJQueryEntityController;
import com.acooly.core.common.web.support.JsonEntityResult;
import com.acooly.core.common.web.support.JsonListResult;
import com.lora.entity.AlarmList;
import com.lora.service.AlarmListService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.LongPredicate;

/**
 * lora_alarm_list 管理控制器
 * 
 * @author qiudot
 * Date: 2019-10-05 12:02:39
 */
@Controller
@RequestMapping(value = "/manage/lora/alarmList")
public class AlarmListManagerController extends AbstractJQueryEntityController<AlarmList, AlarmListService> {
	@Resource
	public JdbcTemplate jdbcTemplate;
	{
		allowMapping = "*";
	}

	@SuppressWarnings("unused")
	@Autowired
	private AlarmListService alarmListService;

	@RequestMapping(value = "alarmList")
	public String alarmList(){
		return "/manage/lora/alarmlist/alarmList";
	}

	@RequestMapping(value = "show")
	public String show(Model model,Long id){
		AlarmList list=alarmListService.get(id);
		model.addAttribute("alarmList",list);
		return "/manage/lora/alarmlist/alarmListShow";
	}
	@RequestMapping(value = "queryList")
	@ResponseBody
	public JsonListResult<Map<String, Object>> queryList(Model model){
		JsonListResult<Map<String, Object>> result=new JsonListResult<>();
		List<Map<String, Object>> maps = jdbcTemplate.queryForList("SELECT * FROM `lora_alarm_list` where handle_status=0 limit 5");
		List<Map<String, Object>> list= jdbcTemplate.queryForList("SELECT count(1) as count FROM `lora_alarm_list` where handle_status=0");
		result.setRows(maps);
		result.setTotal(Long.valueOf(list.get(0).get("count").toString()));
		return result;
	}
	@RequestMapping(value = "alarmListedit")
	public String alarmListedit(Model model,Long id){
		AlarmList list=alarmListService.get(id);
		model.addAttribute("alarmList",list);
		return "/manage/lora/alarmlist/alarmListEdit";
	}
	@RequestMapping(value = "alarmListUpdate")
	@ResponseBody
	public JsonEntityResult<AlarmList> alarmListUpdate(Model model, HttpServletRequest request, Long id){
		JsonEntityResult<AlarmList> result=new JsonEntityResult<>();
		String handleStatus=request.getParameter("handleStatus");
		int s=Integer.valueOf(handleStatus);
		AlarmList list=new AlarmList();
		list.setHandleStatus(s);
		list.setId(id);
		alarmListService.update(list);

		result.setSuccess(true);
		return result;
	}

	@RequestMapping(value = "addcamera")
	@ResponseBody
	public boolean addcamera(Model model, HttpServletRequest request){
		String ip = request.getParameter("ip");
		ip=ip.substring(ip.indexOf("@")+1);
		ip=ip.substring(0,ip.indexOf(":"));
		AlarmList alarmList=new AlarmList();
		alarmList.setFieldName("camera");
		alarmList.setEpId(ip);
		alarmList.setFieldTitle("摄像头");
		alarmList.setHandleStatus(0);
		alarmList.setAlarmInfo("ip为"+ip+"的摄像头无法链接");
		alarmListService.save(alarmList);
		return true;
	}

	@ResponseBody
	@RequestMapping(value = "handleState")
	public JsonEntityResult<String> handleState(Model model, HttpServletRequest request) {
		JsonEntityResult<String> result = new JsonEntityResult<>();
		String ids = request.getParameter("ids");
		for(String id : ids.split(",")) {
			AlarmList alarmList = getEntityService().get(Long.parseLong(id));
			alarmList.setHandleStatus(1);
			getEntityService().update(alarmList);
		}
		return result;
	}

}
